#include "rpc.h"



namespace  chenrpc
{
Channels::Channels():_idx(0){}
Channels::~Channels(){}

void Channels::insert(std::string& addr)
{
    std::unique_lock<std::mutex> lock(_mtx);
    //1,实例化channel对象
    auto channel = std::make_shared<brpc::Channel>();
    brpc::ChannelOptions options;
    options.protocol = "baidu_std";
    channel->Init(addr, &options);
    //2,将对象添加到map中
    _maps.insert(std::make_pair(addr, channel));
    //3,将对象添加到vector中，方便轮询
    _channels.push_back(channel);
}


void Channels::remove(const std::string& addr)
{
    std::unique_lock<std::mutex> lock(_mtx);
    //1,从map中删除channel
    auto it = _maps.find(addr);
    if(it == _maps.end())
    {
        //没有找到节点
        return;
    }

    //从map中移除channel
    for(auto& channel :_channels)
    {
        if(channel == it->second)
        {
            _channels.erase(channel);
            break;
        }
    }

    _maps.erase(it);
}   

}